public class Test {
    public static void main(String[] args) {
        //创建数组
        student[] stu=new student[3];
        //初始化变量
        student s1=new student(1,"yueqiang",21);
        student s2=new student(2,"yixiang",20);
        student s3=new student(3,"haolong",23);
        //存入数组
        stu[0]=s1;
        stu[1]=s2;
        stu[2]=s3;
        //检测
        //Printf(stu);

        //添加学生
        student s4=new student(4,"sb",18);
        //调用id_exit判断id是否存在
        boolean flag=id_exist(stu,s4.getId());
        if(flag){
            System.out.println("id已经存在，无需在存");
        }else{
            //id不存在，进行存数据
            //调用is_space判断空间是否够
            int count=is_space(stu);
            if(count==stu.length){
                //调用add_space开辟空间
                student[] stu1=add_space(stu);
                stu1[count]=s4;
                int len= stu1.length;
                Printf(stu1,len);

            }else{
                stu[count]=s4;
                int len= stu.length;
                Printf(stu,len);
            }
        }
        //要删除的id=4
        //判断id是否存在
        boolean flag1=id_exist(stu,2);
        if(flag1==true){
            //判断下表
            int ret=xia_biao(stu,2);
            //删除该位置元素
             student[] newstu=del(stu,ret);
             int len1= newstu.length;
             Printf(newstu,len1-1);
        }else{
            System.out.println("id不存在");
        }
//查询id
        boolean flag2=id_exist(stu,2);
        if(flag2==true){
            //判断下表
            int ret=xia_biao(stu,2);
            int age= age_add(stu, ret);
            System.out.println(age);
        }else{
            System.out.println("id不存在");
        }


    }
    public static int age_add(student[] arr,int ret){
        int age=arr[ret].getAge()+1;
        return age;
    }
//删除元素
    public static student[] del(student[] arr,int ret){
        for(int i=ret;i<arr.length-ret;i++){
            arr[i]=arr[i+1];
        }
        return arr;
    }

//要删除的下表
    public static  int xia_biao(student[] arr,int Id){
        int con=0;
        int i=0;
        while(arr[i].getId()!=Id){
            con++;
            i++;
        }
        return con;
    }


    //开辟空间
    public static student[] add_space(student[] arr){
        //创建一个新数组
        student[] newstu=new student[arr.length+1];
        //把原数组内容存到新数组里边
        for(int i=0;i<arr.length;i++){
            student stu1=arr[i];
            newstu[i]=stu1;
        }
        return newstu;
    }
    //判断空间是否足够
    public static int is_space(student[] arr) {
        int con = 0;
                for(int i=0;i<arr.length;i++){
                    student stu1=arr[i];
                    if(stu1!=null){
                        con++;
                    }
                }
        return con;
    }

    //判断id是否存在
    public static boolean id_exist(student[] arr,int Id){
        for(int i=0;i<arr.length;i++){
            student stu1=arr[i];
            if(stu1.getId()==Id){
                return true;
            }
        }
        return false;
    }

    //打印
    public static void Printf(student [] arr,int len){
        for(int i=0;i<len;i++){
            student stu1=arr[i];
            System.out.println(stu1.getId()+" "+stu1.getName()+" "+stu1.getAge());
        }
    }
}